import { getToken } from '@/utils/auth'

export function sendChatMessage({ prompt, chatId }) {
  const baseURL = import.meta.env.VITE_APP_BASE_API || ''
  return fetch(baseURL + '/ai/chat?prompt=' + prompt + '&chatId=' + chatId, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': getToken() ? 'Bearer ' + getToken() : undefined
    }
  })
}

export function getChatHistoryIds() {
  const baseURL = import.meta.env.VITE_APP_BASE_API || ''
  return fetch(baseURL + '/ai/history', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': getToken() ? 'Bearer ' + getToken() : undefined
    }
  })
    .then(res => res.json())
    .then(res => res.data || [])
}

export function getChatHistoryDetail(chatId) {
  const baseURL = import.meta.env.VITE_APP_BASE_API || ''
  return fetch(baseURL + '/ai/history/' + chatId, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': getToken() ? 'Bearer ' + getToken() : undefined
    }
  })
    .then(res => res.json())
    .then(res => res.data || [])
}

export function deleteChatHistory(chatId) {
  const baseURL = import.meta.env.VITE_APP_BASE_API || ''
  return fetch(baseURL + '/ai/history/' + chatId, {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': getToken() ? 'Bearer ' + getToken() : undefined
    }
  }).then(res => res.json())
}